package com.demo2do.boyle.client.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.boyle.client.service.CustomerService;
import com.demo2do.boyle.common.entity.customer.Customer;
import com.demo2do.boyle.common.entity.customer.CustomerTag;
import com.demo2do.boyle.common.entity.customer.Source;
import com.demo2do.boyle.common.entity.customer.message.CustomerMessage;
import com.demo2do.boyle.common.entity.customer.wallet.Wallet;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.service.impl.GenericServiceImpl;

/**
 * 
 * @author Wilson/Leo
 */
@Service("customerService")
public class CustomerServiceImpl extends GenericServiceImpl<Customer> implements CustomerService {

    @Autowired
    private GenericDaoSupport genericDaoSupport;

    /*
     * (non-Javadoc)
     * @see com.demo2do.boyle.client.service.CustomerService#loadByOpenId(java.lang.String)
     */
    @SuppressWarnings("unchecked")
    public Customer loadByOpenId(String openId) {
        List<Customer> customers = genericDaoSupport.searchForList("FROM Customer WHERE disabled = false AND openId = :openId", "openId", openId);
        return CollectionUtils.isEmpty(customers) ? null : customers.get(0);
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.boyle.client.service.CustomerService#loadByPhone(java.lang.String)
     */
    @SuppressWarnings("unchecked")
    public Customer loadByMobile(String mobile) {
        List<Customer> customers = genericDaoSupport.searchForList("FROM Customer WHERE disabled = false AND mobile = :mobile", "mobile", mobile);
        return CollectionUtils.isEmpty(customers) ? null : customers.get(0);
    }
    

    /* (non-Javadoc)
     * @see com.demo2do.boyle.backend.service.CustomerService#create(com.demo2do.boyle.common.entity.customer.Customer)
     */
    public void create(Customer customer) {
    	
    	//create customerTag
    	CustomerTag customerTag = new CustomerTag(customer);
    	
    	// get customer code
        String customerCode = generateCustomerCode(customer.getSource());
        
        //create wallet
        Wallet wallet = new Wallet(customer);
        
        genericDaoSupport.save(customer.initialize(customer.getSource(), customerCode, customerTag, wallet));

    }
    
    /**
     * 
     * @param source
     * @return
     */
    @SuppressWarnings("unchecked")
    private String generateCustomerCode(Source source) {

        Integer sequence = 1;
        
        List<String> result = genericDaoSupport.searchForList("SELECT code FROM Customer WHERE code LIKE '" + source.getAlphabet() + "%' ORDER BY code DESC", 0, 1);
        
        if(!result.isEmpty()) {
            
            // get code from database search
            String code = result.get(0);
            
            if(code != null)
                sequence = NumberUtils.createBigDecimal(code.substring(1)).intValue() + 1;
        }
        
        // left pad zero chars
        return source.getAlphabet() + StringUtils.leftPad(sequence.toString(), 5, "0");
        
    }

	@Override
	public void updateMessageStaus(List<CustomerMessage> customerMessages) {
		
		Map<String, Object> parameter = new HashMap<String,Object>();
		for (CustomerMessage customerMessage : customerMessages) {
			parameter.put("readed", !customerMessage.isReaded());
			genericDaoSupport.executeHQL("update CustomerMessage set readed =:readed where readed = false ", parameter);
		}
	}

}
